package cn.stylefeng.guns.modular.api.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.api.entity.Farm;
import cn.stylefeng.guns.modular.api.model.params.FarmParam;
import cn.stylefeng.guns.modular.api.model.params.PoodParam;
import cn.stylefeng.guns.modular.api.model.result.FarmResult;
import cn.stylefeng.guns.modular.api.service.FarmService;
import cn.stylefeng.guns.modular.api.service.PoodService;
import cn.stylefeng.guns.modular.api.vo.FarmStatVo;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.mapstruct.MapMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Api;


/**
 * 养殖场控制器
 *
 * @author wusonghe
 * @Date 2019-08-31 00:07:13
 */
@Controller
@Api(tags="养殖场管理")
@RequestMapping("/api/farm")
public class FarmController extends BaseController {

    private String PREFIX = "/farm";

    @Autowired
    private FarmService farmService;

    @Autowired
    private PoodService poodService;



    /**
     * 跳转到主页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/farm.html";
    }

    /**
     * 新增页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/farm_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/farm_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/addItem")
    @ResponseBody
    @ApiOperation(value = "新增接口", notes = "新增", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "新增", response = ResponseData.class)})
    public ResponseData addItem(FarmParam farmParam) {
        this.farmService.add(farmParam);
        return ResponseData.success();
    }

    /**
     * 更新接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/editItem")
    @ResponseBody
    @ApiOperation(value = "更新接口", notes = "更新", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "更新", response = ResponseData.class)})
    public ResponseData editItem(FarmParam farmParam) {
        this.farmService.update(farmParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除接口", notes = "删除", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "详情", response = ResponseData.class)})
    public ResponseData delete(FarmParam farmParam) {
        this.farmService.delete(farmParam);

        PoodParam poodParam = new PoodParam();
        poodParam.setFarmId(farmParam.getFarmId());
        this.poodService.delete(poodParam);
        return ResponseData.success();
    }
    /**
     * 统计信息
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @PostMapping("/statistics")
    @ResponseBody
    @ApiOperation(value = "统计接口", notes = "统计 - 计划/实际产量", response = FarmStatVo.class)
    @ApiResponses({@ApiResponse(code = 200, message = "统计", response = FarmStatVo.class)})
    public ResponseData statistics(@ModelAttribute("养殖场ID") Integer farmId,
                                   @ModelAttribute("时间 - 比如 - 2019-01") String dateTime) {

        FarmStatVo farmStatVo = farmService.statistics(farmId,dateTime);
        return ResponseData.success(farmStatVo);
    }


    /**
     * 查看详情接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/detail")
    @ResponseBody
    @ApiOperation(value = "详情接口", notes = "详情", response = FarmResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "详情", response = FarmResult.class)})
    public ResponseData detail(FarmParam farmParam) {
        Farm detail = this.farmService.getById(farmParam.getFarmId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @PostMapping(value = "/list")
    @ResponseBody
    @ApiOperation(value = "列表接口", notes = "列表", response = FarmResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "列表", response = FarmResult.class)})
    public LayuiPageInfo list(FarmParam farmParam) {
        return this.farmService.findPageBySpec(farmParam);
    }

}


